﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI
{
    public partial class Application : System.Web.UI.MasterPage
    {
        public const string SESSION_USER = "session_user";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Load data
                LoadBranches();
                //Control access
                ControlAccess();
            }
        }

        public void LoadBranches()
        {
            Utilities.LoadDropDownListNoSelect(ddlBranch, Branches.GetAllBranches().DefaultView, Branches.ColumnNames.Name, Branches.ColumnNames.ID);
            ddlBranch.SelectedValue = Branches.CurrentBranchID.ToString();
            updateBranch.Update();
        }

        protected void ddlBranch_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            Branches branch = new Branches();
            branch.LoadByPrimaryKey(int.Parse(ddlBranch.SelectedValue));
            Branches.CurrentBranch = branch;

            BranchRole role = BranchRole.GetBranchRoleByBranchID(branch.ID);
            if (role.RowCount == 0)
            {
                role.AddNew();
                role.Sales = true;
                role.Purchase = true;
                role.Products = true;
                role.Report = true;
                role.Repair = true;
                role.Customers = true;
                role.Suppliers = true;
                role.Admin = true;
                role.SystemAdmin = branch.IsHeadQuarter;
                role.Contracts = true;
                role.EditSales = true;
                role.Expense = true;
                role.BranchID = branch.ID;
            }
            BranchRole.CurrentBranchRole = role;

            Response.Redirect(this.Page.Request.Url.ToString());
        }

        protected void ControlAccess()
        {
            View_Users user = (View_Users)Session[SESSION_USER];

            if (user.IsColumnNull(View_Users.ColumnNames.SystemAdmin) || !user.SystemAdmin)
                mnuApp.Items[8].ChildItems.RemoveAt(2);
            
            if (!Branches.CurrentBranch.IsHeadQuarter)
            {
                mnuApp.Items[5].ChildItems[0].ChildItems.Clear();
            }

            if (!BranchRole.CurrentBranchRole.Admin || !user.Admin)
                mnuApp.Items.RemoveAt(8);

            if (!BranchRole.CurrentBranchRole.Expense || !user.Expense)
                mnuApp.Items.RemoveAt(7);

            if (!BranchRole.CurrentBranchRole.Report || !user.Report)
                mnuApp.Items.RemoveAt(6);
            else if (!BranchRole.CurrentBranchRole.Admin || !user.Admin)
                    mnuApp.Items[6].ChildItems.RemoveAt(0);

            if (!BranchRole.CurrentBranchRole.Suppliers || !user.Suppliers)
                mnuApp.Items[5].ChildItems.RemoveAt(1);

            if (!BranchRole.CurrentBranchRole.Customers || !user.Customers)
                mnuApp.Items[5].ChildItems.RemoveAt(0);
            
            if(mnuApp.Items[5].ChildItems.Count == 0)
                mnuApp.Items.RemoveAt(5);
            if (!BranchRole.CurrentBranchRole.Products || !user.Products)
                mnuApp.Items.RemoveAt(4);
            //if (!BranchRole.CurrentBranchRole.Repair || !user.Repair)
                mnuApp.Items.RemoveAt(3);
            if (!BranchRole.CurrentBranchRole.Purchase || !user.Purchase)
                mnuApp.Items.RemoveAt(2);
            //if (!BranchRole.CurrentBranchRole.Contracts || !user.Contracts)
                mnuApp.Items.RemoveAt(1);
            if (!BranchRole.CurrentBranchRole.Sales || !user.Sales)
                mnuApp.Items.RemoveAt(0);

            if (user.IsColumnNull(View_Users.ColumnNames.SystemAdmin) || !user.SystemAdmin)
            {
                ddlBranch.Visible = false;
                lblSwitch.Visible = false;
            }
        }

        protected void lnkLogout_OnClick(object sender, EventArgs e)
        {
            Session[SESSION_USER] = null;
            Session.Abandon();
            Session.Clear();
            Server.Transfer("Default.aspx");
        }

        public void SetSysUser(String str)
        {
            lblUser.Text = str;
        }

        public void SetSysDate(String str)
        {
            lblSysDate.Text = str;
        }
    }
}
